package connection
{
	import com.maclema.mysql.MySqlResponse;

	import flash.events.Event;

	public class MyConnection
	{
		import com.maclema.mysql.Statement;
		import com.maclema.mysql.Connection;
		import com.maclema.mysql.ResultSet;
		import mx.controls.Alert;
		import mx.rpc.AsyncResponder;
		import com.maclema.mysql.MySqlToken;
		import com.maclema.util.ResultsUtil;
		//The MySql Connection
		private var con:Connection;
		private var sqlCode:String=""
		private var isConnected:Boolean=false;
		
		//private var host:String="moomserver";    			// Host 
		private var host:String="";  		  				// Host = "leandro-pc"
		private var port:int=0;					     		// Porta padrão do MySql = 3306
		private var username:String="";         		// Username do Banco = "root"
		private var password:String="";       		// Senha do Banco = "111111"
		private var database:String="";		// Nome da Base de Dados = "GUESTSMANAGEMENT"

		private var resultFunction:Function;
		private var faultFunction:Function;
		private var connectFunction:Function;

		public var lastResultSet:ResultSet;
		public var lastToken:MySqlToken;
		public var lastResultResponse:MySqlResponse

		public function setPropriertiesConnetion(host:String="", port:int=0, username:String="", password:String="", database:String=""):void{
			this.host = host;
			this.port = port;
			this.username = username;
			this.password = password;
			this.database = database;
		}

		public function getConnection(connectFunction:Function=null):void {
			this.connectFunction = connectFunction;
			con = new Connection(this.host, this.port, this.username, this.password, this.database);
			con.addEventListener(Event.CONNECT, handleConnected);
			con.connect();
		}

		private function handleConnected(e:Event):void {
			this.isConnected=true;
			if ( this.connectFunction!= null ){
				this.connectFunction()
			}
		}

		public function executeSql(sqlCode:String, resultFunction:Function=null, faultFunction:Function=null, info:String="default"):Boolean {
			if ( !this.isConnected ) {
				Alert.show("Erro 000#000 - Sem conexão ativa!!!");
				return false;
			}

			this.resultFunction = resultFunction;
			this.faultFunction = faultFunction;

			var st:Statement = con.createStatement();
			st.sql = sqlCode;

			var token:MySqlToken = st.executeQuery();
			token.info = info;
			token.addResponder(new AsyncResponder(result, fault, token));
			return true;
		}

		private function result(data:Object, token:Object):void {
			if ( data is ResultSet ){ // Para Select
				this.lastResultSet = ResultSet(data);
			} else if ( data is MySqlResponse ){
				this.lastResultResponse = MySqlResponse(data);
			}
			this.lastToken = MySqlToken(token);
			if (this.resultFunction!=null){
				this.resultFunction();
			}
		}

		private function fault(info:Object, token:Object):void {
			if (this.faultFunction!=null){
				this.faultFunction(info.toString());
			}
		}


	}
}